<?php

function show_json($msg) {
    exit(json_encode($msg));
}

function collect($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //不直接输出获取的数据
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);   //连接超时，3秒如果连接不成功，则超时
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); //尝试重连超时时间，单位毫秒
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 执行超时(秒)，一定要加上此项，否则有时可能因$sUrl访问不成功导致进程阻塞
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:28.0) Gecko/20100101 Firefox/28.0');
    $r = curl_exec($ch);
    /*
     *$error = curl_errno($rCh);
     *var_dump($error);
     */
    curl_close($ch);


    return $r;
}

$_GET['url'] = 'http://mp.weixin.qq.com/s?__biz=MzIyMTcwNjc2MQ==&mid=2247483653&idx=1&sn=c198a7d052df011cd61b2308e6d17956&chksm=e839ee63df4e67755ef7a8df424bbab4aff1749ce5c1d98e127d7ea32895af3fe366eb20d7f6&mpshare=1&scene=23&srcid=12217pyVf96MPYU4KLPJxsEO#rd';


$url = empty($_GET['url']) ? '' : trim($_GET['url']);

if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
    $url = 'http://' . urldecode($url);
}

if (empty($url)) {
    show_json(['s' => false, 'm' => '请指定待检测网址']);
}

if (!$html = collect($url)) {
    show_json(['s' => false, 'm' => '指定网址中没有获取到内容']);
}

$regx = '#<p>(此内容因违规无法查看)</p>#';
preg_match($regx, $html, $tmp);

$r = empty($tmp)
     ? ['s' => true,  'm' => '指定网址内容正常']
     : ['s' => false, 'm' => $tmp[1]];

show_json($r);
